<?php
/**
 * 费用
 */
declare (strict_types = 1);
namespace app\sk\service;

use app\sk\tpl\ViewIndex;
use app\sk\tpl\ViewAdd;
use app\sk\tpl\ViewEdit;
use app\core\Relation;
use app\core\Res;

class ActionType
{

    /**
     * 
     * @param $params
     * @return $data
     */
    public static function indexView($aData){

        $field_model= Relation::getModel('table_action_field');
        $fData=Res::allData($field_model,['action_id'=>$aData['id']]);

        $title_model= Relation::getModel('table_action_title');
        $tData=Res::allData($title_model,['action_id'=>$aData['id']]);

        $mtableurl='/sk/index/list/actionid/'.$aData['id'];
        $exporturl='/sk/index/export/actionid/'.$aData['id'];
        $cols='var cols =[';
        $model=new ViewIndex();
        foreach($fData as $items){
            if($items['field_show']==1){
                $p=[
                    'type'=>'input',
                    'inputType'=>'text',
                    'titleName'=>$items['field_name_zh'],
                    'inputName'=>$items['field_name'],
                    'verType'=>'lay-verType="tips"',
                ];
                if($items['field_verify']==1){
                    $p['xinhao']='*';
                    $p['verify']='lay-verify="required" required';
                }
                if($items['field_type_id']<6){
                    $p['onkeyup']=$model->onKeyupMoney();
                }
                $model->setFormInput($p);
            }

            if($items['field_tbshow']==1){
                $cols.='{field: "'.$items['field_name'].'", minWidth: 100,align:"center",title: "'.$items['field_name_zh'].'",sort:true,filter: true},';
            }
            

        }

        $titlebar='';
        $titleevent='';

        $TbBar='';
        $TbEvent='';

        foreach($tData as $titems){
            $tname='btn'.$titems['title_name'];
            $tzhname=$titems['title_name_zh'];
            $title_url=$titems['title_url'];
            if($titems['title_type']==1){
                $titlebar.=
<<<EOF
'<button lay-event="$tname" id="$tname" class="layui-btn layui-btn-sm icon-btn">$tzhname</button>&nbsp;',
EOF;

$titleevent.=
<<<EOF
if (obj.event === '$tname') { // 添加
    let layIndex = layer.open({
        type: 2,
        title: '$tzhname',
        shade: 0.5,
        fixed: false,
        area: ['80%','80%'],
        content: "$title_url",
        success: function (layero, index) {
            if(mData){
                var iframe = window['layui-layer-iframe' + index];//拿到iframe元素
                iframe.child(JSON.stringify(mData))//向此iframe层方法 传递参数
            }
        }
    });
}
EOF;
            }

            if($titems['title_type']==2){
                if($titems['title_type_act']==2){
                    $title_url='/sk/index/del/actionid/'.$aData['id'];
                    $TbBar.=
<<<EOF
<a class="layui-btn layui-btn-danger layui-btn-xs" id="$tname" lay-event="$tname">$tzhname</a>
EOF;

$TbEvent.=
<<<EOF
if (obj.event === '$tname') { 
    layer.confirm('确定要删除选中数据吗？', {
        title: '删除数据',
        skin: 'layui-layer-admin',
        shade: 0.5,
        shadeClose: true,
    }, function (i) {
        layer.close(i);
        var loadIndex = layer.load(2);
        $.post("$title_url", {
            id: obj.data ? obj.data.id : '',
            ids: obj.ids ? obj.ids.join(',') : ''
        }, function (res) {
            layer.close(loadIndex);
            if (res.code === 1) {
                layer.msg(res.msg, {icon: 1});
                myTable.reload();
            } else {
                layer.msg(res.msg, {icon: 5, anim: 6});
            }
        }, 'json');
    });
}
EOF;
                }else{
                $TbBar.=
<<<EOF
<a class="layui-btn layui-btn-xs" id="$tname" lay-event="$tname">$tzhname</a>
EOF;

$TbEvent.=
<<<EOF
if (obj.event === '$tname') { // 添加
    let layIndex = layer.open({
        type: 2,
        title: '$tzhname',
        shade: 0.5,
        fixed: false,
        area: ['80%','80%'],
        content: "$title_url",
        success: function (layero, index) {
            if(obj.data){
                var iframe = window['layui-layer-iframe' + index];//拿到iframe元素
                iframe.child(JSON.stringify(obj.data))//向此iframe层方法 传递参数
            }
        }
    });
}
EOF;
                }
            }
        }
        $cols.="{field: 'cdate', title: '创建时间',width: 170,sort:true,filter: true},{fixed: 'right',title: '操作',toolbar: '#authTbBar',align:'center', minWidth: 150}];";
        $model->setJsCols($cols);
        $model->setMtableUrl($mtableurl);
        $model->setExportUrl($exporturl);
        
        $model->setToolbar($titlebar);
        $model->setTitleEvent($titleevent);

        $model->setTbBar($TbBar);
        $model->setTbEvent($TbEvent);
        $model->view();
    }

    /**
     * @param $params
     * @return $data
     */
    public static function addView($aData){

        $field_model= Relation::getModel('table_action_field');
        $fData=Res::allData($field_model,['action_id'=>$aData['id']]);

        $mtableurl='/sk/index/add/actionid/'.$aData['id'];
        
        $model=new ViewAdd();
        foreach($fData as $items){
            if($items['field_show']==1){
                $p=[
                    'type'=>'input',
                    'inputType'=>'text',
                    'titleName'=>$items['field_name_zh'],
                    'inputName'=>$items['field_name'],
                    'verType'=>'lay-verType="tips"',
                ];
                if($items['field_verify']==1){
                    $p['xinhao']='*';
                    $p['verify']='lay-verify="required" required';
                }
                if($items['field_type_id']<6){
                    $p['onkeyup']=$model->onKeyupMoney();
                }

                if($items['field_type_id']==11 || $items['field_type_id']==12){
                    $jsdate=$model->selectJsDate1($p);
                    $model->setJsLaydate($jsdate);
                }
                $model->setFormInput($p);
            }

            
            

        }
        
        $model->setaddUrl($mtableurl);
        $model->view();
    }

    /**
     * @param $params
     * @return $data
     */
    public static function editView($aData){

        $field_model= Relation::getModel('table_action_field');
        $fData=Res::allData($field_model,['action_id'=>$aData['id']]);

        $mtableurl='/sk/index/edit/actionid/'.$aData['id'];
        
        $model=new ViewEdit();
        foreach($fData as $items){
            if($items['field_show']==1){
                $p=[
                    'type'=>'input',
                    'inputType'=>'text',
                    'titleName'=>$items['field_name_zh'],
                    'inputName'=>$items['field_name'],
                    'verType'=>'lay-verType="tips"',
                ];
                if($items['field_verify']==1){
                    $p['xinhao']='*';
                    $p['verify']='lay-verify="required" required';
                }
                if($items['field_type_id']<6){
                    $p['onkeyup']=$model->onKeyupMoney();
                }

                if($items['field_type_id']==11 || $items['field_type_id']==12){
                    $jsdate=$model->selectJsDate1($p);
                    $model->setJsLaydate($jsdate);
                }
                $model->setFormInput($p);
            }

            
            

        }
        
        $model->seteditUrl($mtableurl);
        $model->view();
    }


    

    
}